package com.cdxy.dmr.repository;

import com.cdxy.dmr.dataObject.AreaDO;
import com.cdxy.dmr.mapper.AreaMapper;
import com.cdxy.model.dto.base.mybatis.BaseRepository;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author autoGenCode
 * @since 2020-03-05
 */
@Repository
@CacheConfig(cacheNames = "area")
public class AreaRepository extends BaseRepository<AreaMapper, AreaDO> {

    /**
     * 查询所有第一级
     *
     * @return
     */
    @Cacheable(key = "'listAllOneGrade'")
    public List<AreaDO> listAllOneGrade() {
        return this.baseMapper.selectList(this.queryWrapper()
                .eq(AreaDO::getLevel, 1));
    }

    /**
     * 查询所有第一级
     *
     * @return
     */
    @Cacheable(key = "'listAllTwoGrade'")
    public List<AreaDO> listAllTwoGrade() {
        return this.baseMapper.selectList(this.queryWrapper()
                .eq(AreaDO::getLevel, 2));
    }

    /**
     * 通过 parentId 查找
     *
     * @param parentId
     * @return
     */
    @Cacheable(key = "'listByParentId:'+#parentId")
    public List<AreaDO> listByParentId(Integer parentId) {
        return this.baseMapper.selectList(this.queryWrapper()
                .eq(AreaDO::getParentId, parentId));
    }

    /**
     * 随机返回第三级城市
     *
     * @param size
     * @return
     */
    public List<AreaDO> listCityBySizeOrderByRand(Integer size, List<String> city) {
        return this.baseMapper.selectList(this.queryWrapper()
                .notIn(AreaDO::getName, city)
                .eq(AreaDO::getLevel, 2)
                .last("ORDER BY RAND() LIMIT " + size));
    }

}
